/**
 * 工具函数
 */

// 手机号正则验证
function isPhoneNumber(phone) {
    return /^1[3-9]\d{9}$/.test(phone);
}

//密码正则验证
function isPassword(paw) {
    return /^(\w){6,20}$/.test(paw);
}

//弹窗
function toast(icon = "tick", textHint = '操作成功',callback) {
    //创建标签
    let popUp = document.createElement('div');
    popUp.className = 'pop-up';
    // 2. 为元素设置 innerHTML
    popUp.innerHTML = `
        <i class="iconfont icon-${icon}" id="iconfont"></i>
        <span class="mt10">${textHint}</span>
        `
    // 3. 挂载元素
    document.body.appendChild(popUp);
    // 4. 自动销毁元素
    window.setTimeout(function () {
        popUp.remove()
        callback && callback()
    }, 2000)
}

//底部菜单栏
//type===0:首页 1：运动 2：我的
function addTab(type) {
    //创建节点
    let tabWrapper = document.createElement('div');
    tabWrapper.className = 'tab-wrapper'
    tabWrapper.innerHTML = `
                            <a class="dpflex fdcolumn flex1 menu-item ${type===0?'active':''}" href="./index.html">
                                <i class="iconfont icon-home"></i>
                                <span>首页</span>
                            </a>
                            <a class="dpflex fdcolumn flex1 menu-item ${type===1?'active':''}" href="./sports-course.html">
                                <i class="iconfont icon-run"></i>
                                <span>运动</span>
                            </a>
                            <a class="dpflex fdcolumn flex1 menu-item ${type===2?'active':''}" href="./mine.html">
                                <i class="iconfont icon-mine"></i>
                                <span>我的</span>
                            </a>
                            `
    body.appendChild(tabWrapper)
}


// 判断用户是否登录，若没登陆，跳转到登录页
function checkUserLogin() {
    if (localStorage.getItem('user') === null) {
        location.href = './login.html'
    }
}

//补零函数
function addZero(v) {
    return v < 10 ? '0' + v : v;
}

window.utils = {
    isPhoneNumber,
    isPassword,
    addTab,
    toast,
    checkUserLogin,
    addZero,
}